﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DataAccessLayer;
using Entities;
using BusinessLogicLayer;
using BusinessLogicLayer.Validation;

namespace QuanLyTKB_TienDoGD.QuanLyTKBDeNghi
{
    public partial class frmThemChitietTKBDeNghi : Office2007Form
    {
        IRepository<ChiTietTKBDeNghi> _repository;
        ChiTietTKBDeNghiManager _manager;
        ChiTietTKBDeNghi ct;
        frmChiTietTKBDeNghi frmCha;
        bool _isUpdate;
        

        public frmThemChitietTKBDeNghi()
        {
            InitializeComponent();
        }

        public frmThemChitietTKBDeNghi(ChiTietTKBDeNghiManager mana, frmChiTietTKBDeNghi frm, ChiTietTKBDeNghi _ct = null)
            : this()
        {
            _manager = mana;
            ct = _ct;
            _isUpdate = (_ct != null);
            frmCha = frm;
        }

        private void frmThemChitietTKBDeNghi_Load(object sender, EventArgs e)
        {                 
            LoadAllCBB();
            if (_isUpdate)
            {
                txtMaTKBDN.Text = ct.MaTKBDN;
                cbbLopHoc.SelectedValue = ct.MaLop;
                if (ct.GhepLop == "")
                    cbbGhepLop.SelectedIndex = -1;
                else
                    cbbGhepLop.SelectedValue = ct.GhepLop;
                cbbMonHoc.SelectedValue = ct.MaMH;
                cbbGiaoVien.SelectedValue = ct.MaGV;

                nrdLyThuyet.Text = Convert.ToString(ct.SoTietCanXep);
                nrdThucHanh.Text = Convert.ToString(ct.SoTietThucHanhCanXep);
                txtDeNghi.Text = ct.DeNghi;
                
            }
                _repository = new Repository<ChiTietTKBDeNghi>();
                _manager = new ChiTietTKBDeNghiManager(_repository);       
            txtMaTKBDN.ReadOnly = _isUpdate;
        }


        private void LoadCBBLopHoc()
        {
            List<LopHoc> ds = LopHoc.Repository.GetAll().ToList();
            LopHoc lh = new LopHoc();
            lh.MaLop = "------- All ------";
            ds.Insert(0, lh);
            cbbLopHoc.DataSource = ds;
            cbbLopHoc.DisplayMember = "MaLop";
            cbbLopHoc.ValueMember = "MaLop";
            cbbLopHoc.SelectedIndex = 0;
        }

        private void LoadCbbGhepLop()
        {
            List<LopHoc> ds = LopHoc.Repository.GetAll().ToList();
            LopHoc lh = new LopHoc();
            cbbGhepLop.DataSource = ds;
            cbbGhepLop.DisplayMember = "MaLop";
            cbbGhepLop.ValueMember = "MaLop";
            cbbGhepLop.SelectedIndex = -1;
        }

        private void LoadCbbMonHoc()
        {
            List<MonHoc> ds = MonHoc.Repository.GetAll().ToList();
            MonHoc mh = new MonHoc();
            mh.TenMH = "------- All ------";
            ds.Insert(0, mh);
            cbbMonHoc.DataSource = ds;
            cbbMonHoc.DisplayMember = "TenMH";
            cbbMonHoc.ValueMember = "MaMH";
            cbbMonHoc.SelectedIndex = 0;

        }

        public void LoadCbbGiaoVien()
        {
            List<GiaoVien> ds = GiaoVien.Repository.GetAll().ToList();
            GiaoVien gv = new GiaoVien();
            gv.TenGV = "------- All ------";
            ds.Insert(0, gv);
            cbbGiaoVien.DataSource = ds;
            cbbGiaoVien.DisplayMember = "TenGV";
            cbbGiaoVien.ValueMember = "MaGV";
            cbbGiaoVien.SelectedIndex = 0;
        }
        public void LoadAllCBB()
        {
            LoadCBBLopHoc();
            LoadCbbGhepLop();
            LoadCbbMonHoc();
            LoadCbbGiaoVien();
        }

        

        private void btnMacDinh_Click(object sender, EventArgs e)
        {
            txtMaTKBDN.ResetText();
            LoadAllCBB();
            txtDeNghi.ResetText();
            nrdLyThuyet.ResetText();
            nrdThucHanh.ResetText();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ct = new ChiTietTKBDeNghi();

            ct.MaTKBDN = txtMaTKBDN.Text;
            ct.MaLop = cbbLopHoc.SelectedValue.ToString();
            if (cbbGhepLop.SelectedIndex != -1)
                ct.GhepLop = cbbGhepLop.SelectedValue.ToString();
            else ct.GhepLop = "";
            ct.MaMH = cbbMonHoc.SelectedValue.ToString();
            ct.MaGV = cbbGiaoVien.SelectedValue.ToString();
            ct.SoTietCanXep = Convert.ToInt32(nrdLyThuyet.Text);
            ct.SoTietThucHanhCanXep = Convert.ToInt32(nrdThucHanh.Text);
            ct.DeNghi = txtDeNghi.Text;
            ct.MaDN = frmCha.MaDN;

            var result = _manager.Validate(ct);
            if (result.Valid)
            {
                var aresult = _manager.InsertOrUpdate(ct);
                if (aresult.Success)
                {
                    frmCha.btnLamMoi.PerformClick();
                    if (_isUpdate)
                    {
                        MessageBox.Show("Cập nhật chi tiết TKB đề nghị thành công.", "Thông báo");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Thêm chi tiết TKB đề nghị thành công.", "Thông báo");
                        btnMacDinh.PerformClick();
                    }
                    
                }
                else MessageBox.Show(aresult.GetErrosMessages());
            }
            else MessageBox.Show(result.ToString());
        }

    }
}
